package cn.com.ejiakanghu.rabbitmq;

import cn.com.ejiakanghu.core.config.DirectExchangeRabbitMQConfig;
import cn.com.ejiakanghu.core.config.WechatConstant;
import cn.com.ejiakanghu.service.EnterpriseWeChatService;
import cn.com.ejiakanghu.service.NoticeService;
import cn.com.ejiakanghu.service.RfidService;
import cn.ejiakanghu.logistics.service.impl.OrderStateService;
import com.alibaba.fastjson.JSON;
import com.google.common.reflect.TypeToken;
import org.linlinjava.litemall.db.vo.wechat.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author wj
 * @date 2021/10/19 13:47
 */
@Component
public class DirectExchangeConsumer {
    private Logger logger = LoggerFactory.getLogger(DirectExchangeConsumer.class);

    @Resource
    private EnterpriseWeChatService enterpriseWeChatService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private NoticeService noticeService;

    @Resource
    private OrderStateService orderStateService;

    @Resource
    private RfidService rfidService;


    @RabbitListener(queues = DirectExchangeRabbitMQConfig.FROM_NOTICE_TO_LITEMALL)
    @RabbitHandler
    public void getQueueMessage(String msg) {

        logger.info("litemall消息队列获得消息:{}", msg);
        try {
            ToLitemallBean bean = JSON.parseObject(msg, ToLitemallBean.class);
            switch (bean.getType()) {
                case 0:
                    //拒绝
                    String responseCode = stringRedisTemplate.opsForValue().get(WechatConstant.TEMPLATE_RESPONSE_CODE_REDIS_KEY + bean.getId().toString());
                    enterpriseWeChatService.refused(bean.getId(), false, responseCode);
                    break;
                case 1:
                    //接单
                    enterpriseWeChatService.confirm(bean.getId());
                    break;
                default:
                    logger.info("litemall消息队列获得type失败:{}", msg);
                    break;
            }
        } catch (Exception e) {
            logger.error("litemall消息队列报错:{},报错信息:{}", e, msg);
        }
    }


    @RabbitListener(queues = DirectExchangeRabbitMQConfig.FROM_LOGISTICS_TO_NOTICE)
    @RabbitHandler
    public void getLogisticsMessage(String msg) {
        logger.info("notice消息队列获得消息:{}", msg);
        try {
            ToLitemallBean bean = JSON.parseObject(msg, ToLitemallBean.class);
            switch (bean.getType()) {
                case 2:
                    //发送通知
                    List<Integer> missionList = bean.getMissionId();
                    missionList.forEach(missionId -> enterpriseWeChatService.sendNotice(missionId));
                    break;
                case 3:
                    //发送医生通知
                    Long serviceOrderId = bean.getServiceOrderId();
                    enterpriseWeChatService.sendDoctorNotice(serviceOrderId);
                    break;
                default:
                    logger.info("notice消息队列获得type失败:{}", msg);
                    break;
            }
        } catch (Exception e) {
            logger.error("notice消息队列报错:{},报错信息:{}", e, msg);
        }

    }


    @RabbitListener(queues = DirectExchangeRabbitMQConfig.FROM_ROUTING_TO_NOTICE)
    @RabbitHandler
    public Integer getRoutingMessage(String msg) {
//            logger.info("routing消息队列获得消息:{}",msg);
        try {
            if (msg.contains(RabbitmqMsgType.MSG_TYPE_DEPOLY)) {
                Type depolyType = new TypeToken<RabbitmqMsgInfo<ChangeGoodsEquBean>>() {
                }.getType();
                RabbitmqMsgInfo<ChangeGoodsEquBean> depolyBean = JSON.parseObject(msg, depolyType);
                //软链锁刷卡配货
                logger.info("获取routing消息成功DEPORY:{}", depolyBean.getMsg());
                noticeService.changeGoodsEquState(depolyBean.getMsg().getEquId());
                return null;
            } else if (msg.contains(RabbitmqMsgType.MSG_TYPE_RETURN)) {
                Type returnType = new TypeToken<RabbitmqMsgInfo<ChangeGoodsEquBean>>() {
                }.getType();
                RabbitmqMsgInfo<ChangeGoodsEquBean> renturnBean = JSON.parseObject(msg, returnType);
                //软链锁归还 需要判断这个桩是不是围老项目的订单
                ChangeGoodsEquBean returnBean = renturnBean.getMsg();
                rfidService.changeRfidReturn(returnBean.getEquId().toString(), 1, 0);
                return null;
            } else if (msg.contains(RabbitmqMsgType.MSG_TYPE_Print)) {
                Type printType = new TypeToken<RabbitmqMsgInfo<PrintAnnoMsg>>() {
                }.getType();
                RabbitmqMsgInfo<PrintAnnoMsg> printBean = JSON.parseObject(msg, printType);
                logger.info("获取routing消息成功PRINT:{},{}", printBean.getMsg().getIcardRid(),printBean.getMsg().getMac());
                //播报机刷卡打印
                String rfid = printBean.getMsg().getIcardRid();
                String mac = printBean.getMsg().getMac();
                return noticeService.sendAllOrderMsg(rfid,mac);
            } else {
                logger.info("routing消息队列获得type失败:{}", msg);
                return null;
            }
        } catch (Exception e) {
            logger.error("routing消息队列报错:{},报错信息:{}", e, msg);
        }
        return null;
    }

}
